var placeholder = require('placeholder');
var valid = require('{plus}/validate');
var checkradio = require('{plus}/checkradio');
var calendar = require('{plus}/calendar');
var util = require('util');
var request = require('request');
var layer = require('layer');
var router = require('router');
var select = require('{pub}/select');
var events = require('events');

return function(element, _layer){
    var that = this;
    var form = element.find('form');

    return events({
        element:element,
        events:{
            'click .layer-button-confirm':'submit',
            'click [name="createDate"]':'calendar'
        },
        init:function(){

            //初始化单复选框
            element.find(':radio').checkradio({
                callback:function(ele){
                    //选择后隐藏报错提示
                    ele.closest('.ui-item').find('.ui-err').empty()
                }
            });

            //初始化下拉框
            var opts = {
                click:function(ele){
                    ele.closest('.ui-input').removeClass('s-err');
                    ele.closest('.ui-item').find('.ui-err').empty();
                }
            }
            element.find('select').imitSelect(opts);

            select(element.find('[name="industryId"]'), 'account/getIndustryList', '请选择所属行业', opts, {
                list:'industryList',
                value:'id',
                text:'name'
            })
            

            //初始化表单验证
            form.validate({
                errorClass:'s-err',
                onkeyup:false,
                focusInvalid:false,
                onfocusout:false,
                focusCleanup:true,
                ignore:'',
                success:function(error, element) {
                    error.remove();
                    $(element).addClass('s-succ');
                },
                errorPlacement:function(error, element) {
                    element.removeClass('s-succ');
                    if(error.text()){
                        element.closest('.ui-item').find('.ui-err').html(error);
                        if(element.get(0).nodeName === 'SELECT'){
                            element.closest('.ui-input').addClass('s-err')
                        }
                    }
                },
                submitHandler:function(){
                    var data = util.getData(form).result;
                    request.postUpdate('account/create', data, {
                        success:function(res){
                            layer.message();
                            if(_layer){
                                _layer.destroy();
                                that.paging.query('reload')
                            }
                            else{
                                router.location('/list/')
                            }
                        },
                        other:function(res){
                            layer.message('error', res.message);
                        }
                    }, _layer)
                    return false
                }
            })
        },
        submit:function(e, elem){
            form.submit()
        },
        calendar:function(e, elem){
            $.calendar({
                format:'yyyy-MM',
                ismonth:true,
                monthcount:4,
                button:[{
                    name:'today',
                    text:'本月'
                },{
                    name:'close',
                    text:'关闭'
                },{
                    name:'confirm',
                    text:'确定'
                }]
            })
        }
    }).init()
}